package cn.flying.system.service.cm.impl;

import javax.annotation.Resource;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.system.common.enums.StatusEnum;
import cn.flying.system.domain.cm.CmDictCode;
import cn.flying.system.domain.cm.CmDictCodeExample;
import cn.flying.system.mapper.cm.CmDictCodeMapper;
import cn.flying.system.service.cm.CmDictCodeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @description:
 * @author: lvyong
 * @date: 2022年05月22日 19:00
 * @version: 1.0
 */
@Service
public class CmDictCodeServiceImpl implements CmDictCodeService {

    @Resource
    private CmDictCodeMapper cmDictCodeMapper;

    /**
     * 分页查询字典枚举
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageResponse<CmDictCode> getDictCodePageList(PageRequest<CmDictCode> pageRequest) {
        Integer pageNum = pageRequest.getPageNum();
        Integer pageSize = pageRequest.getPageSize();
        CmDictCode cmDictCode = pageRequest.getParam();
        CmDictCodeExample example = new CmDictCodeExample();
        CmDictCodeExample.Criteria criteria = example.createCriteria();
        if (StringUtils.hasText(cmDictCode.getDictType())) {
            criteria.andDictTypeEqualTo(cmDictCode.getDictType());
        }
        if (StringUtils.hasText(cmDictCode.getStatus())) {
            criteria.andStatusEqualTo(cmDictCode.getStatus());
        }
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        Page<CmDictCode> page = (Page<CmDictCode>) cmDictCodeMapper.selectByExample(example);
        return new PageResponse(page.getTotal(), pageNum, page.getResult());
    }

    /**
     * 根据字典类型查询枚举码值列表
     *
     * @param type
     * @return
     */
    @Override
    public List<CmDictCode> getDictCodeList(String type) {
        CmDictCodeExample example = new CmDictCodeExample();
        CmDictCodeExample.Criteria criteria = example.createCriteria();
        criteria.andDictTypeEqualTo(type);
        criteria.andStatusEqualTo(StatusEnum.STATUS_Y.getCode());
        return cmDictCodeMapper.selectByExample(example);
    }

    /**
     * 主键Id查询枚举
     *
     * @param id
     * @return
     */
    @Override
    public CmDictCode getDictCodeById(Long id) {
        return cmDictCodeMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增保存
     *
     * @param cmDictCode
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertDictCode(CmDictCode cmDictCode) {
        if (StringUtils.hasText(cmDictCode.getStatus()) && "on".equals(cmDictCode.getStatus())) {
            cmDictCode.setStatus("Y");
        } else {
            cmDictCode.setStatus("N");
        }
        cmDictCodeMapper.insertSelective(cmDictCode);
    }

    /**
     * 根据主键ID更新
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDictCodeById(CmDictCode cmDictCode) {
        if (StringUtils.hasText(cmDictCode.getStatus()) && "on".equals(cmDictCode.getStatus())) {
            cmDictCode.setStatus("Y");
        } else {
            cmDictCode.setStatus("N");
        }
        cmDictCodeMapper.updateByPrimaryKeySelective(cmDictCode);
    }

    /**
     * 通过主键ID删除
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDictCodeById(Long id) {
        cmDictCodeMapper.deleteByPrimaryKey(id);
    }
}
